package org.easy.alists;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({StsProperties.class})
@AutoConfigureAfter
@Slf4j
public class StsAutoConfiguration implements InitializingBean {
    private final StsProperties stsProperties;

    public StsAutoConfiguration(StsProperties stsProperties) {
        this.stsProperties = stsProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    public AliyunStsService aliyunOssService() {
        return new AliyunStsServiceImpl(stsProperties.getStsEndpoint(), stsProperties.getAccessKeyId(),
                stsProperties.getAccessKeySecret(), stsProperties.getRoleArn());
    }

    @Override
    public void afterPropertiesSet() {
        checkParam();
    }

    private void checkParam() {
        if (stsProperties.getAccessKeyId() == null ||
                stsProperties.getAccessKeySecret() == null ||
                stsProperties.getRoleArn() == null
        ) {
            log.error("未配置" + StsProperties.class.getTypeName());
        }

    }

}
